//
//  CodeViewModel.m
//  AppleMarket
//
//  Created by yangy on 24/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "CodeViewModel.h"

// Model
#import "ImageCodeMoel.h"

@implementation CodeViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        [self.refershCodeCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                self.imageModel = [ImageCodeMoel yy_modelWithDictionary:[x objectForKey:@"Data"]];
                [self.refershEndCodeSubject sendNext:nil];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        }];
        [[_refershCodeCommand.executing skip:1] subscribeError:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
    }
    return self;
}

- (RACCommand *)refershCodeCommand
{
    if(!_refershCodeCommand){
        _refershCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_imageCodeWithType:input completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _refershCodeCommand;
}

- (RACSubject *)refershEndCodeSubject
{
    if(!_refershEndCodeSubject){
        _refershEndCodeSubject = [RACSubject subject];
    }
    return _refershEndCodeSubject;
}

- (RACSubject *)closeSubject
{
    if(!_closeSubject){
        _closeSubject = [RACSubject subject];
    }
    return _closeSubject;
}

- (RACSubject *)completeSubject
{
    if(!_completeSubject){
        _completeSubject = [RACSubject subject];
    }
    return _completeSubject;
}

@end
